import QtQuick 1.1
import com.nokia.meego 1.0
import ATND 1.0
import './Pages/'
import './APIs/'
import './Components/'

PageStackWindow {
    id: root

    EventFeed {
        id: eventFeed
    }

    DateTime {
        id: dateTime
    }

    Connections {
        target: mainPage
        onStatusChanged: {
            if (mainPage.status === PageStatus.Active) {
                model.event_id = 0
            }
        }
    }

    EventModel {
        id: model
        onStatusChanged: {
            console.debug('status', status, XmlListModel.Ready)
            if (status === XmlListModel.Ready) {
                console.debug('model.count', model.count)
                if (model.count == 1) {
                    if (pageStack.depth == 1)
                        pageStack.push(eventPage, {'event': get(0)})
                    else
                        pageStack.replace(eventPage, {'event': get(0)})
                }
            }
        }
    }

    function open(urls) {
        console.debug(urls)
        if (urls.length === 1) {
            var url = urls[0]
            var rgx = new RegExp(/^(http:\/\/atnd\.org\/events|atnd:\/)\/(\d+)\/?$/)
            var ret
            console.debug(url, rgx.exec(url))
            if ((ret = rgx.exec(url)) !== null) {
//                console.debug(model.event_id, ret[2])
                var event_id = ret[2]
                model.event_id = event_id
            }
        }
    }

    function reload() {
        console.debug('reload')
    }

    QtObject {
        id: constants
        property int defaultMargin: 4

        property int iconLargeSize: 48
        property int iconDefaultSize: 32
        property int iconSmallSize: 26

        property int fontLargeSize: 36
        property int fontDefaultSize: 32
        property int fontSmallSize: 26
    }

    Settings {
        id: settings
        identifier: 'QATND'
        version: '0.0.0'
    }

    initialPage: mainPage

    MainPage { id: mainPage }
    Component { id: eventPage; EventPage{} }
    Component { id: aboutPage; AboutPage{} }
    Component { id: splashPage; SplashPage{} }
}
